package com.example.qa_back.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qa_back.entity.CollectQuestion;
import com.example.qa_back.entity.Response;
import com.example.qa_back.service.BackCQService;
import com.example.qa_back.utils.HandleExcelExport;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 董帅
 * @date 2021/3/7 - 12:18
 */
@RestController
@RequestMapping("/api/backCQ")
public class BackCQController {
    private final BackCQService backCQService;

    public BackCQController(BackCQService backCQService) {
        this.backCQService = backCQService;
    }

    /*
    * 功能：分页获取Q
    * */
    @PostMapping("/getCQ")
    public Response getCQ(@RequestBody Map<String, Integer> map) {
        Integer page = map.get("page");
        Integer num = map.get("num");
        Page<CollectQuestion> questionPage = backCQService.getPage(page, num);
        return new Response(1, "获取成功", questionPage);
    }

    /*
    * 功能：单个删除Q
    * */
    @PostMapping("/deleteCQ")
    public Response deleteCQ(@RequestBody Map<String, Integer> map) {
        Integer id = map.get("id");
        Integer integer = backCQService.deleteCQ(id);
        if (integer == 1) {
            return new Response(1, "删除成功", null);
        } else {
            return new Response(0, "删除失败", null);
        }
    }

    /*
    * 功能：批量删除Q
    * */
    @PostMapping("/deleteManyCQ")
    public Response deleteManyCQ(@RequestBody Map<String, Integer[]> map) {
        Integer[] ids = map.get("ids");
        Integer integer = backCQService.deleteManyCQ(ids);
        if (integer == 1) {
            return new Response(1, "删除成功", null);
        } else {
            return new Response(0, "删除失败", null);
        }
    }

    /*
    * 功能：导出Q
    * */
    @GetMapping("/exportCQ")
    public Response exportCQ() {
        String filePath = "F:\\q_a\\file\\CQ.xlsx";
//        String filePath = "/root/q_a/CQ.xlsx";
        List<String> titles = new ArrayList<>();
        titles.add("question");
        List<CollectQuestion> allCQ = backCQService.getAllCQ();
        Integer integer = HandleExcelExport.exportCQs(filePath, titles, allCQ, "CQ");
        if (integer == 1) {
            return new Response(1, "导出成功", null);
        } else {
            return new Response(0, "导出失败", null);
        }
    }
}
